<?php

namespace mybackend\controllers\monitor;

use Yii;
use yii\data\Pagination;
use mybackend\controllers\BaseController;
use common\entities\myhtsq\ActionCode;
use publiccode\entities\monitor\MonitorPoolEntity;
use publiccode\services\monitor\MonitorPoolService;

class MonitorPoolController extends BaseController {

    public $layout = '@app/views/monitor/layouts/main.php';

    public function __construct($id, $module, $config = []) {
        parent::__construct($id, $module, $config);
    }

//    protected function roleAuthRules() {
//        return [
//            'actions' => ['Index', 'Edit', 'Delete', 'QuickSet'],
//            'rules' => [
//                [
//                    'allow' => true,
//                    'actionCodeMaps' => [
//                        'Index' => ActionCode::CMS_ALL,
//                        'Edit' => ActionCode::CMS_ALL,
//                        'Delete' => ActionCode::CMS_ALL,
//                        'QuickSet' => ActionCode::CMS_ALL,
//                    ]
//                ]
//            ]
//        ];
//    }

    public function actionIndex() {
        if (Yii::$app->request->isAjax) {
            $keyword = Yii::$app->request->get('keyword', '');
            $key = Yii::$app->request->get('key', '');
            $status = Yii::$app->request->get('status', '');
            $page = Yii::$app->request->get('page', 1);
            $page_size = Yii::$app->request->get('rows', 20);
            $sidx = Yii::$app->request->get('sidx', 'id');
            $sord = Yii::$app->request->get('sord', 'DESC');
            $data = [
                'total' => 1,
                'page' => $page,
                'records' => 0,
                'rows' => [],
            ];
            $query = MonitorPoolEntity::find();
            if ($keyword) {
                $query->andWhere(['=', 'id', $keyword]);
            }
            if ($key != '') {
                $query->andWhere(['=', 'redis_key', $key]);
            }
            if ($status != '') {
                $query->andWhere(['=', 'status', $status]);
            }
            $pages = new Pagination(['totalCount' => $query->count()]);
            $pages->setPageSize($page_size, true);
            foreach ($query->orderBy($sidx . ' ' . $sord)->offset($pages->offset)->limit($pages->limit)->all() as $key => $item) {
                $data['records'] = $pages->totalCount;
                $data['total'] = ceil($pages->totalCount / $page_size);
                $data['rows'][$key] = [
                    'id' => $item->id,
                    'cell' => [
                        $item->id,
                        $item->redis_key,
                        $item->redis_data,
                        $item->retry_count,
                        $item->error_msg,
                        $item->statusFmt(),
                        $item->update_date,
                    ]
                ];
            }
            echo json_encode($data);
            return Yii::$app->end();
        }

        return $this->render('index');
    }

    public function actionView($id) {
        $model = MonitorPoolEntity::find()->where(['id' => $id])->one();
        $data = [
            'model' => $model,
        ];
        return $this->render('view', $data);
    }

    public function actionRetry($id) {
        $model = MonitorPoolEntity::find()->where(['id' => $id])->one();
        if ($model->canRetry()) {
            $service = new MonitorPoolService();
            $service->retry($id);
        }
        $this->redirect('?r=/monitor/monitor-pool/view&id=' . $id);
    }

}
